/* 
 -----------------------------------------------------------------------------
                   Cogaen - Component-based Game Engine (v3)
 -----------------------------------------------------------------------------
 This software is developed by the Cogaen Development Team. Please have a 
 look at our project home page for further details: http://www.cogaen.org
    
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 Copyright (c) 2010 Roman Divotkey

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/

package org.cogaen.demo.state;

import java.awt.Color;
import java.awt.event.KeyEvent;

import org.cogaen.core.Core;
import org.cogaen.event.Event;
import org.cogaen.event.EventListener;
import org.cogaen.event.EventManager;
import org.cogaen.event.EventType;
import org.cogaen.event.SimpleEvent;
import org.cogaen.input.KeyPressedEvent;
import org.cogaen.java2d.ImageHandle;
import org.cogaen.java2d.Overlay;
import org.cogaen.java2d.SceneManager;
import org.cogaen.java2d.SpriteHandle;
import org.cogaen.resource.ResourceManager;
import org.cogaen.state.GameState;
import org.cogaen.task.FireEventTask;
import org.cogaen.task.TaskManager;

public class SplashState implements GameState, EventListener {
	
	public static final String NAME = "Splash";
	public static final EventType END_OF_SPLASH = new EventType("EndOfSplash");	
	private static final String RES_GROUP = NAME;
	private static final double DEFAULT_DELAY = 3.0;
	private Core core;
	private double delay;

	public SplashState(Core core) {
		this(core, DEFAULT_DELAY);
	}
	
	public SplashState(Core core, double delay) {
		this.core = core;
		this.delay = delay;
		
		// initialize resource group
		ResourceManager resMngr = ResourceManager.getInstance(this.core);
		resMngr.addResource(SplashState.RES_GROUP, new ImageHandle("splash_img", "cogaen_mit_1280x800.jpg"));
		resMngr.addResource(SplashState.RES_GROUP, new SpriteHandle("splash_spr", "splash_img"));
	}
	
	@Override
	public String getName() {
		return NAME;
	}

	@Override
	public void onEnter() {
		// register as event listener for key pressed events in order to quit before timeout
		EventManager.getInstance(this.core).addListener(this, KeyPressedEvent.TYPE);		
		
		// create task sending EndOfSpalsh event
		FireEventTask task = new FireEventTask(this.core, "SplashKiller", new SimpleEvent( END_OF_SPLASH ), this.delay);
		TaskManager.getInstance(this.core).attachTask( task );
		
		// load resource group
		ResourceManager.getInstance(this.core).loadGroup(RES_GROUP);
		
		// create overlay displaying splash image
		SceneManager scnMngr = SceneManager.getInstance(this.core);
		scnMngr.getScreen().hideMouseCursor();
		scnMngr.setClearBackground(true);
		scnMngr.setBackgroundColor(Color.BLACK);
		Overlay splashOvl = scnMngr.createOverlay();		
		splashOvl.addVisual( scnMngr.createSpriteVisual("splash_spr") );	
		scnMngr.centerOverlay(splashOvl);
	}

	@Override
	public void onExit() {
		TaskManager.getInstance(this.core).killTask("SplashKiller");
		EventManager.getInstance(this.core).removeListener(this);
		SceneManager.getInstance(this.core).destroyAllOverlays();
		ResourceManager.getInstance(this.core).unloadGroup(RES_GROUP);
	}

	@Override
	public void handleEvent(Event event) {
		if ( event.isOfType(KeyPressedEvent.TYPE) ) {
			KeyPressedEvent keyPressed = (KeyPressedEvent) event;
			if (keyPressed.getKeyCode() == KeyEvent.VK_ESCAPE) {
				EventManager.getInstance(this.core).enqueueEvent( new SimpleEvent(END_OF_SPLASH) );
			}
		}
	}

}
